<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>video</title>
    <style>
        html,
        body {
            margin: 0;
        }
        #btn {
            left: 50%;
            top: 50%;
            position: absolute;
            z-index: 100;
            width: 200px;
            height: 50px;
        }
    </style>
</head>

<body>
    <canvas class="webgl"></canvas>
    <button id="btn">开始</button>
    <video 
        id="video"
        src="./pikachu.mp4"
        playsinline
        webkit-playsinline
        autoplay
        loop
        style="display:none"
        ></video>
    <script src="https://cdn.jsdelivr.net/npm/dat.gui@0.7.7/build/dat.gui.js"></script>
    <script type="module">
        import * as THREE from '../../source/three.module.js';
        import { OrbitControls } from '../../source/OrbitControls.js';

        const sizes = {
            width: window.innerWidth,
            height: window.innerHeight
        }

        const canvas = document.querySelector(".webgl");

        // 场景
        const scene = new THREE.Scene();

        // 相机
        const camera = new THREE.PerspectiveCamera(
            75,
            sizes.width / sizes.height,
            0.1,
            1000
        )
        camera.position.x = 0
        camera.position.y = 0
        camera.position.z = 5
        scene.add(camera);

        // 添加光照
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5)
        scene.add(ambientLight)

        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5)
        directionalLight.position.set(2, 2, -1)

        scene.add(directionalLight)

        // 控制器
        const controls = new OrbitControls(camera, canvas);
        scene.add(camera);

        /* 核心逻辑 */
        // 添加屏幕
        const video = document.getElementById( 'video' );
        video.play();
        const texture = new THREE.VideoTexture( video );

        const geometry = new THREE.BoxGeometry(5, 5, 0.2);
        const cubeMaterial = new THREE.MeshStandardMaterial({
            // color: '#ff0000'
            map: texture
        });
        const cubeMesh = new THREE.Mesh(geometry, cubeMaterial);
        scene.add(cubeMesh);

         /* 核心结束 */


        window.addEventListener('resize', () => {
            // 设置大小
            sizes.width = window.innerWidth
            sizes.height = window.innerHeight

            // 更新相机
            camera.aspect = sizes.width / sizes.height
            camera.updateProjectionMatrix()

            // 更新渲染器
            renderer.setSize(sizes.width, sizes.height)
            renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
        })

        btn.onclick = () => {
            video.play();
            btn.style.display = "none";
        }

        // 设置渲染器
        const renderer = new THREE.WebGLRenderer({
            canvas
        })
        // 设置大小
        renderer.setSize(sizes.width, sizes.height)
        // 设置像素比
        renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
        renderer.render(scene, camera);

        const tick = () => {
            controls.update();

            renderer.render(scene, camera)
            texture.needsUpdate = true;

            window.requestAnimationFrame(tick)
        }

        tick();

    </script>
</body>

</html>